package ru.spbsu.electromenu.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.spbsu.electromenu.storage.Storage;

import java.util.List;

/**
 * Date: 3/20/11
 * Time: 1:12 PM
 *
 * @author Artem Titov
 */
public abstract class AbstractService<T, S extends Storage<T>> {
    protected final static Logger logger = LoggerFactory.getLogger(AbstractService.class);
    protected S storage;

    public void setStorage(final S storage) {
        this.storage = storage;
    }

    public T get(final int id) {
        logger.debug("Get item with id {} from the storage", id);
        return storage.get(id);
    }

    public int put(final T object) {
        logger.debug("Put item to the storage");
        return storage.put(object);
    }

    public int update(final T object) {
        logger.debug("Update item in the storage");
        return storage.update(object);
    }

    public int delete(final int id) {
        logger.debug("Delete item with id {} from the storage", id);
        return storage.delete(id);
    }

    public List<T> getAll() {
        logger.debug("Get all items from the storage");
        final List<T> result = storage.getALl();
        logger.debug("{} items are loaded", result.size());
        return result;
    }
}
